namespace Digitalmes.Application.Mes;

/// <summary>
/// 设备表服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "mes")]
public sealed class EquipItemService : IDynamicWebApiService
{
    private readonly SugarRepository<EquipItem> _thisRepository;
    private readonly SugarRepository<MdStation> _stationRepo;

    public EquipItemService(SugarRepository<EquipItem> thisRepository, SugarRepository<MdStation> stationRepo)
    {
        _thisRepository = thisRepository;
        _stationRepo = stationRepo;
    }

    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<EquipItemDto>> GetPagesAsync(PageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .WhereIF(!string.IsNullOrWhiteSpace(param.Key), s => s.EquipCode.Contains(param.Key) || s.EquipName.Contains(param.Key))
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<EquipItemDto>>();
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<EquipItemDto> GetAsync(long id)
    {
        var model = await _thisRepository.GetByIdAsync(id);
        return model.Adapt<EquipItemDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> AddAsync(EquipItemDto model)
    {
        if (await _thisRepository.IsAnyAsync(s => s.EquipCode == model.EquipCode && s.LineId == model.LineId))
        {
            Oops.ThrowBah(ErrCode.E4002);
        }

        var item = await FillAsync(model);
        return await _thisRepository.InsertAsync(item);
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(EquipItemDto model)
    {
        if (await _thisRepository.IsAnyAsync(s => s.EquipCode == model.EquipCode && s.LineId == model.LineId && s.Id != model.Id))
        {
            Oops.ThrowBah(ErrCode.E4002);
        }

        var item = await FillAsync(model);
        return await _thisRepository.UpdateAsync(item);
    }

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync([FromBody] List<long> ids) =>
        await _thisRepository.DeleteAsync(m => ids.Contains(m.Id));

    /// <summary>
    /// 补全设备的其他信息
    /// </summary>
    private async Task<EquipItem> FillAsync(EquipItemDto model)
    {
        // 补全产线信息
        var station = await _stationRepo.AsQueryable()
                        .Includes(s => s.Line)
                        .FirstAsync(s => s.Id == model.OpId);

        var item = model.Adapt<EquipItem>();
        item.OpCode = station.OpCode;
        item.LineId = station.LineId;
        item.LineCode = station.Line?.LineCode;

        return item;
    }
}
